import moment from 'moment/moment';

const tableOption = {
  border: true,
  addBtn: false,
  viewBtn: false,
  editBtn: false,
  delBtn: false,
  index: true,
  menu: true,
  columnBtn: false,
  refreshBtn: false,
  indexLabel: '序号',
  column: [
    {
      label: '统计周期',
      prop: 'statisticalDate'
    },
    // {
    //   label: '工单号',
    //   prop: 'workOrderNo'
    // },
    {
      label: '产品编码',
      prop: 'productPartNo'
    },
    {
      label: '产品型号',
      prop: 'productPartModel'
    },
    {
      label: '工单批数',
      prop: 'workOrderCount'
    },
    {
      label: '生产总数',
      prop: 'completedQuantity'
    },
    {
      label: '目标直通率',
      prop: 'goalFpy',
      span: 7,
      formatter: (val) => {
        if (val.goalFpy) {
          return val.goalFpy + '%';
        } else {
          return '0%';
        }
      }
    },
    {
      label: '实际直通率',
      prop: 'actualFpy',
      formatter: (val) => {
        if (val.actualFpy) {
          return val.actualFpy + '%';
        } else {
          return '0%';
        }
      }
    }
  ]
};

const getOption = (self) => {
  return {
    menuPosition: 'right',
    submitBtn: false,
    emptyBtn: false,
    column: [
      {
        label: '统计维度',
        prop: 'statisticsTimeDimension',
        type: 'radio',
        button: true,
        span: 4,
        value: 'DAY',
        dicData: [
          {
            label: '日',
            value: 'DAY'
          },
          {
            label: '周',
            value: 'WEEK'
          },
          {
            label: '月',
            value: 'MONTH'
          }
        ],
        control: (val, form) => {
          if (form.statisticsTimeDimension === 'WEEK') {
            return {
              weekStart: {
                display: true
              },
              weekEnd: {
                display: true
              },
              examineDate: {
                display: false
              },
              month: {
                display: false
              }
            };
          } else if (form.statisticsTimeDimension === 'DAY') {
            return {
              weekStart: {
                display: false
              },
              weekEnd: {
                display: false
              },
              examineDate: {
                display: true
              },
              month: {
                display: false
              }
            };
          } else {
            return {
              examineDate: {
                display: false
              },
              weekStart: {
                display: false
              },
              weekEnd: {
                display: false
              },
              month: {
                display: true
              }
            };
          }
        }
      },
      {
        label: '时间范围',
        span: 6,
        prop: 'examineDate',
        type: 'daterange',
        format: 'yyyy-MM-dd',
        valueFormat: 'yyyy-MM-dd',
        startPlaceholder: '开始日期',
        endPlaceholder: '结束日期',
        rules: [
          {
            required: true,
            message: '请选择时间范围',
            trigger: 'blur'
          },
          {
            trigger: 'change',
            validator: (rule, value, callback) => {
              const endTime = value[1];
              const startTime = value[0];
              if (moment(endTime).diff(moment(startTime), 'day') >= 31) {
                callback(new Error('查询时间最多选择31天'));
              }
              callback();
            }
          }
        ]
      },
      {
        label: '起始周',
        span: 6,
        prop: 'weekStart',
        type: 'week',
        format: 'yyyy 第 WW 周',
        valueFormat: 'yyyy-MM-dd',
        rules: [
          {
            required: true,
            message: '请选择起始周范围',
            trigger: 'blur'
          }
        ]
      },
      {
        label: '结束周',
        span: 6,
        prop: 'weekEnd',
        type: 'week',
        format: 'yyyy 第 WW 周',
        valueFormat: 'yyyy-MM-dd',
        rules: [
          {
            required: true,
            message: '请选择结束周范围',
            trigger: 'blur'
          },
          {
            trigger: 'change',
            validator: self.validateWeek
          }
        ]
      },
      {
        label: '时间范围',
        span: 6,
        prop: 'month',
        type: 'monthrange',
        format: 'yyyy-MM',
        startPlaceholder: '开始月份',
        endPlaceholder: '结束月份',
        valueFormat: 'yyyy-MM-dd',
        rules: [
          {
            required: true,
            message: '请选择时间范围',
            trigger: 'blur'
          },
          { validator: self.validateDateRange, trigger: 'change' }
        ]
      },
      {
        formslot: true,
        prop: 'contain',
        span: 6,
        display: true
      },
      {
        label: '',
        prop: 'btn',
        span: 24,
        formslot: true
      }
    ]
  };
};

//不同表格的option
const getTableOption = (type) => {
  const columMap = {
    factory: [
      {
        label: '统计周期',
        prop: 'statisticalDate'
      },
      {
        label: '工厂名称',
        prop: 'deptName'
      },
      {
        label: '工单批数',
        prop: 'workOrderCount'
      },
      {
        label: '生产总数',
        prop: 'completedQuantity'
      },
      {
        label: '目标直通率',
        prop: 'goalFpy',
        span: 7,
        formatter: (val) => {
          if (val.goalFpy) {
            return val.goalFpy + '%';
          } else {
            return '0%';
          }
        }
      },
      {
        label: '实际直通率',
        prop: 'actualFpy',
        formatter: (val) => {
          if (val.actualFpy) {
            return val.actualFpy + '%';
          } else {
            return '0%';
          }
        }
      }
    ],
    workshop: [
      {
        label: '统计周期',
        prop: 'statisticalDate',
      },
      {
        label: '车间名称',
        prop: 'workshopName'
      },
      {
        label: '工单批数',
        prop: 'workOrderCount'
      },
      {
        label: '生产总数',
        prop: 'completedQuantity'
      },
      {
        label: '目标直通率',
        prop: 'goalFpy',
        span: 7,
        formatter: (val) => {
          if (val.goalFpy) {
            return val.goalFpy + '%';
          } else {
            return '0%';
          }
        }
      },
      {
        label: '实际直通率',
        prop: 'actualFpy',
        formatter: (val) => {
          if (val.actualFpy) {
            return val.actualFpy + '%';
          } else {
            return '0%';
          }
        }
      }
    ],
    product: [
      {
        label: '统计周期',
        prop: 'statisticalDate'
      },
      // {
      //   label: '工单号',
      //   prop: 'workOrderNo'
      // },
      {
        label: '产品编码',
        prop: 'productPartNo'
      },
      {
        label: '产品型号',
        prop: 'productPartModel'
      },
      {
        label: '工单批数',
        prop: 'workOrderCount'
      },
      {
        label: '生产总数',
        prop: 'completedQuantity'
      },
      {
        label: '目标直通率',
        prop: 'goalFpy',
        span: 7,
        formatter: (val) => {
          if (val.goalFpy) {
            return val.goalFpy + '%';
          } else {
            return '0%';
          }
        }
      },
      {
        label: '实际直通率',
        prop: 'actualFpy',
        formatter: (val) => {
          if (val.actualFpy) {
            return val.actualFpy + '%';
          } else {
            return '0%';
          }
        }
      }
    ],
    productLine: [
      {
        label: '统计周期',
        prop: 'statisticalDate'
      },
      {
        label: '产线名称',
        prop: 'lineName'
      },
      {
        label: '所属车间',
        prop: 'workshopName'
      },
      {
        label: '工单批数',
        prop: 'workOrderCount'
      },
      {
        label: '生产总数',
        prop: 'completedQuantity'
      },
      {
        label: '目标直通率',
        prop: 'goalFpy',
        span: 7,
        formatter: (val) => {
          if (val.goalFpy) {
            return val.goalFpy + '%';
          } else {
            return '0%';
          }
        }
      },
      {
        label: '实际直通率',
        prop: 'actualFpy',
        formatter: (val) => {
          if (val.actualFpy) {
            return val.actualFpy + '%';
          } else {
            return '0%';
          }
        }
      }
    ]
  };
  return columMap[type];
};

// 不同维度的标识参数
const getStatisticType = (type) => {
  const map = {
    workshop: 'WORK_SHOP',
    product: 'PRODUCT',
    team: 'SHIFT_GROUP',
    productLine: 'LINE',
    factory: 'FACTORY'
  };
  return map[type];
};
const getStartDay = (time, type) => {
  if (type === 'WEEK') {
    return moment(time).startOf('isoWeek').format('yyyy-MM-DD');
  } else {
    return moment(time).startOf('month').format('yyyy-MM-DD');
  }
};

const getEndDay = (time, type) => {
  if (type === 'WEEK') {
    return moment(time).endOf('isoWeek').format('yyyy-MM-DD');
  } else {
    return moment(time).endOf('isoWeek').format('yyyy-MM-DD');
  }
};

const valiDateFlied = (from, type) => {
  const validateMap = {
    workshop: 'workshopId',
    product: 'partMasterId',
    productLine: 'lineId'
  };
  const field = validateMap[type];
  return type === 'factory' ? true : from[field] && from[field].length;
};

const detailOption = {
  submitBtn: false,
  emptyBtn: false,
  group: [
    {
      icon: 'el-icon-vertical-bar',
      label: '工单信息',
      collapse: true,
      arrow: false,
      prop: 'group1',
      span: 8,
      column: [
        {
          label: '工单号',
          prop: 'workOrderNo',
          span: 8,
          disabled: true
        },
        {
          label: '产品编码',
          prop: 'productPartNo',
          span: 8,
          disabled: true
        },
        {
          label: '产品型号',
          prop: 'productPartModel',
          span: 8,
          disabled: true
        },
        {
          label: '生产数量',
          prop: 'completedQuantity',
          span: 8,
          disabled: true
        },
        {
          label: '目标直通率',
          span: 8,
          prop: 'goalFpy',
          disabled: true,
          slot: true
        }
      ]
    },
    {
      icon: 'el-icon-vertical-bar',
      label: '工段详情',
      collapse: true,
      arrow: false,
      prop: 'group2',
      column: [
        {
          label: '',
          labelWidth: '0',
          prop: 'table',
          span: 24
        }
      ]
    }
  ]
};
const getFormOption = (type) => {
  const columMap = {
    factory: [
      {
        label: '工厂名称',
        prop: 'deptName',
        disabled: true,
        span: 8
      },
      {
        label: '统计周期',
        prop: 'statisticalDate',
        disabled: true,
        span: 8
      },
      {
        label: '产线数',
        prop: 'lineCount',
        disabled: true,
        span: 8
      },
      {
        label: '工单批数',
        prop: 'workOrderCount',
        disabled: true,
        span: 8
      },
      {
        label: '生产总数',
        prop: 'completedQuantity',
        disabled: true,
        span: 8
      },
      {
        label: '目标直通率',
        prop: 'goalFpy',
        slot: true,
        disabled: true,
        span: 8
      }
    ],
    workshop: [
      {
        label: '车间名称',
        prop: 'workshopName',
        span: 8,
        disabled: true
      },
      {
        label: '统计周期',
        prop: 'statisticalDate',
        span: 8,
        disabled: true
      },
      {
        label: '产线数',
        prop: 'lineCount',
        span: 8,
        disabled: true
      },
      {
        label: '工单批数',
        prop: 'workOrderCount',
        span: 8,
        disabled: true
      },
      {
        label: '生产总数',
        prop: 'completedQuantity',
        span: 8,
        disabled: true
      },
      {
        label: '目标直通率',
        prop: 'goalFpy',
        span: 8,
        slot: true,
        disabled: true
      }
    ],
    product: [
      {
        label: '工单号',
        prop: 'workOrderNo',
        disabled: true,
        span: 8
      },
      {
        label: '产品编码',
        prop: 'productPartNo',
        disabled: true,
        span: 8
      },
      {
        label: '产品型号',
        prop: 'productPartModel',
        disabled: true,
        span: 8
      },
      {
        label: '生产数量',
        prop: 'completedQuantity',
        disabled: true,
        span: 8
      },
      {
        label: '目标直通率',
        prop: 'goalFpy',
        slot: true,
        disabled: true,
        span: 8
      }
    ],
    productLine: [
      {
        label: '产线名称',
        prop: 'lineName',
        disabled: true,
        span: 8
      },
      {
        label: '所属车间',
        prop: 'workshopName',
        disabled: true,
        span: 8
      },
      {
        label: '统计周期',
        prop: 'statisticalDate',
        disabled: true,
        span: 8
      },
      {
        label: '工单批数',
        prop: 'workOrderCount',
        disabled: true,
        span: 8
      },
      {
        label: '生产总数',
        prop: 'completedQuantity',
        disabled: true,
        span: 8
      },
      {
        label: '目标直通率',
        prop: 'goalFpy',
        slot: true,
        disabled: true,
        span: 8
      }
    ]
  };
  return columMap[type];
};

const detailTableOption = {
  border: true,
  addBtn: false,
  viewBtn: false,
  editBtn: false,
  delBtn: false,
  index: true,
  menu: false,
  columnBtn: false,
  refreshBtn: false,
  indexLabel: '序号',
  column: [
    {
      label: '工段',
      prop: 'sectionName'
    },
    {
      label: '产线',
      prop: 'lineName'
    },
    {
      label: '生产数量',
      prop: 'completedQuantity'
    },
    {
      label: '目标直通率',
      prop: 'goalFpy',
      formatter: (val) => {
        if (val.goalFpy) {
          return val.goalFpy + '%';
        } else {
          return '0%';
        }
      }
    },
    {
      label: '实际直通率',
      prop: 'actualFpy',
      formatter: (val) => {
        if (val.actualFpy) {
          return val.actualFpy + '%';
        } else {
          return '0%';
        }
      }
    }
  ]
};
const getDetailTableOption = (type) => {
  const columMap = {
    factory: [
      {
        label: '产线名称',
        prop: 'lineName'
      },
      {
        label: '工单号',
        prop: 'workOrderNo'
      },
      {
        label: '产品编码',
        prop: 'productPartNo'
      },
      {
        label: '产品型号',
        prop: 'productPartModel'
      },
      {
        label: '生产数量',
        prop: 'completedQuantity'
      },
      {
        label: '开始时间',
        prop: 'startTime'
      },
      {
        label: '完成时间',
        prop: 'endTime'
      },
      {
        label: '目标直通率',
        prop: 'goalFpy',
        formatter: (val) => {
          if (val.goalFpy) {
            return val.goalFpy + '%';
          } else {
            return '0%';
          }
        }
      },
      {
        label: '实际直通率',
        prop: 'actualFpy',
        formatter: (val) => {
          if (val.actualFpy) {
            return val.actualFpy + '%';
          } else {
            return '0%';
          }
        }
      }
    ],
    workshop: [
      {
        label: '产线名称',
        prop: 'lineName'
      },
      {
        label: '工单号',
        prop: 'workOrderNo'
      },
      {
        label: '产品编码',
        prop: 'productPartNo'
      },
      {
        label: '产品型号',
        prop: 'productPartModel'
      },
      {
        label: '生产数量',
        prop: 'completedQuantity'
      },
      {
        label: '开始时间',
        prop: 'startTime'
      },
      {
        label: '完成时间',
        prop: 'endTime'
      },
      {
        label: '目标直通率',
        prop: 'goalFpy',
        span: 7,
        formatter: (val) => {
          if (val.goalFpy) {
            return val.goalFpy + '%';
          } else {
            return '0%';
          }
        }
      },
      {
        label: '实际直通率',
        prop: 'actualFpy',
        formatter: (val) => {
          if (val.actualFpy) {
            return val.actualFpy + '%';
          } else {
            return '0%';
          }
        }
      }
    ],
    product: [
      {
        label: '工段',
        prop: 'sectionName'
      },
      {
        label: '产线',
        prop: 'lineName'
      },
      {
        label: '生产数量',
        prop: 'completedQuantity'
      },
      {
        label: '目标直通率',
        prop: 'goalFpy',
        formatter: (val) => {
          if (val.goalFpy) {
            return val.goalFpy + '%';
          } else {
            return '0%';
          }
        }
      },
      {
        label: '实际直通率',
        prop: 'actualFpy',
        formatter: (val) => {
          if (val.actualFpy) {
            return val.actualFpy + '%';
          } else {
            return '0%';
          }
        }
      }
    ],
    productLine: [
      {
        label: '工单号',
        prop: 'workOrderNo'
      },
      {
        label: '产品编码',
        prop: 'productPartNo'
      },
      {
        label: '产品型号',
        prop: 'productPartModel'
      },
      {
        label: '生产数量',
        prop: 'completedQuantity'
      },
      {
        label: '开始时间',
        prop: 'actualStartTime'
      },
      {
        label: '完成时间',
        prop: 'actualEndTime'
      },
      {
        label: '目标直通率',
        prop: 'goalFpy',
        span: 7,
        formatter: (val) => {
          if (val) {
            return val.goalFpy + '%';
          } else {
            return '0%';
          }
        }
      },
      {
        label: '实际直通率',
        prop: 'actualFpy',
        formatter: (val) => {
          if (val) {
            return val.actualFpy + '%';
          } else {
            return '0%';
          }
        }
      }
    ]
  };
  return columMap[type];
};

export { tableOption, getOption, getStatisticType, getStartDay, getEndDay, getTableOption, valiDateFlied, detailOption, getFormOption, detailTableOption, getDetailTableOption };
